#
# Copyright 2009 Silas Sewell
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import sys
import huck.api.keyvalue
import huck.api.session
import huck.api.user
from huck.webapp import *

class Application(WSGIApplication):

  def _handler_setup(self, handler):
    handler.keyvalue = huck.api.keyvalue.get(handler)
    handler.session = huck.api.session.get(handler)
    handler.user = huck.api.user.get(handler)

  def _handler_destroy(self, handler):
    pass

def run_appengine(application):
  from google.appengine.ext.webapp.util import run_wsgi_app
  sys.stdout = sys.stderr
  run_wsgi_app(application)

def run_dev(address, port, application):
  from wsgiref.simple_server import make_server
  serve = make_server(address, port, application)
  try:
    serve.serve_forever()
  except KeyboardInterrupt:
    serve.socket.close()

def run_fcgi(address, port, application):
  from flup.server.fcgi import WSGIServer as FCGI
  FCGI(application, bindAddress=(address, port)).run()

def run_scgi(address, port, application):
  from flup.server.scgi_fork import WSGIServer as SCGI
  SCGI(application=application, bindAddress=(address, port)).run()
 
def run_wsgi(application):
  sys.stdout = sys.stderr
  return application
